/*
 * @Author: wangdazhuang
 * @Date: 2024-08-29 19:08:07
 * @LastEditTime: 2025-01-27 16:08:06
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/views/player/common_player_page.dart
 */
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:media_kit_video/media_kit_video.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/views/player/controllers/common_video_play_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/views/player/views/av_player_loading.dart';
import 'views/media_kit_custom_controls.dart' as custom;

class CommonPlayerPage extends StatelessWidget {
  const CommonPlayerPage({super.key});

  ///web上推出全屏后的自动播放
  void _autoPlay(CommonVideoPlayerController _, BuildContext context) {
    Timer.periodic(
      const Duration(milliseconds: 1000),
      (timer) {
        _.player?.play();
        timer.cancel();
      },
    );
  }

  _buildPlayer(CommonVideoPlayerController _) {
    if (!_.playerInitialized || _.player == null) {
      return const AvPlayerLoading();
    }
    final context = Get.context!;
    final videoH = Get.height;
    final themColor = COLOR.hexColor("#ff960f");

    final controls = MaterialVideoControlsTheme(
      normal: MaterialVideoControlsThemeData(
        seekBarPositionColor: themColor,
        seekBarThumbColor: themColor,
      ),
      fullscreen: MaterialVideoControlsThemeData(
        seekBarPositionColor: themColor,
        seekBarThumbColor: themColor,
      ),
      child: custom.MediaKitCustomControls(
        buildContext: context,
        viewSize: Size(Get.width, videoH),
        texturePos: Rect.fromLTWH(0, 0, Get.width, videoH),
      ),
    );
    return kIsWeb
        ? Video(
            key: const Key('c-video-player'),
            controller: _.playerVC!,
            aspectRatio: _.asp,
            filterQuality: FilterQuality.none,
            controls: (__) => Stack(
              children: [controls],
            ),
            onEnterFullscreen: () async {
              _autoPlay(_, context);
            },
            onExitFullscreen: () async {
              _autoPlay(_, context);
            },
          )
        : Video(
            key: const Key('c-video-player'),
            controller: _.playerVC!,
            filterQuality: FilterQuality.none,
            aspectRatio: _.asp,
            controls: (__) => Stack(
              children: [controls],
            ),
          );
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: Scaffold(
        backgroundColor: COLOR.black,
        body: GetBuilder<CommonVideoPlayerController>(
          builder: (vc) {
            if (!vc.playerInitialized) return const AvPlayerLoading();
            return Center(
              child: SizedBox(
                height: Get.height,
                child: _buildPlayer(vc),
              ),
            );
          },
        ),
      ),
    );
  }
}
